package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.*;
import org.csu.mypetstore.persistence.impl.*;

import java.util.List;

public class CatalogService {
    private CategoryDAO categoryDAO;
    private ProductDAO productDAO;
    private ItemDAO itemDAO;
    private CartListDAO cartListDAO;
    
    public CatalogService(){
        categoryDAO = new CategoryDAOImpl();
        productDAO = new ProductDAOImpl();
        itemDAO = new ItemDAOImpl();
        cartListDAO = new CartListDAOImpl();
    }

    public List<Category> getCategoryList() {
        return categoryDAO.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryDAO.getCategory(categoryId);
    }

    public Product getProduct(String productId) {
        return productDAO.getProduct(productId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productDAO.getProductListByCategory(categoryId);
    }

    // TODO enable using more than one keyword
    public List<Product> searchProductList(String keyword) {
        return productDAO.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public String SearchAjax(String keyword){
        String res = "";
        List<Product> productList = productDAO.searchProductList("%" + keyword.toLowerCase() + "%");
        for (int i=0 ;i<productList.size();i++){
            if (i>0){
                res += ","+productList.get(i).getProductId();
            }else {
                res += productList.get(i).getProductId();
            }
        }
        return res;
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemDAO.getItemListByProduct(productId);
    }

    public Item getItem(String itemId) {
        return itemDAO.getItem(itemId);
    }

    public boolean isItemInStock(String itemId) {
        return itemDAO.getInventoryQuantity(itemId) > 0;
    }

    public void insertCartList(CartList cartList) {
        cartListDAO.insertCartList(cartList);
    }

    public void deleteCartListByUsername(String username){
        cartListDAO.deleteCartByUsername(username);
    }

    public List<CartList> getCartListByUsername(String username) {
        return cartListDAO.getCartListByUsername(username);
    }

    public void deleteCartItemByUsernameAndItemid(String username,String itemid){
        cartListDAO.deleteCartItemByUsernameAndItemId(username,itemid);
    }




}
